#include "foo.hpp"
#include <unistd.h>

pthread_cond_t cond2;
pthread_cond_t cond3;
pthread_mutex_t mutex;

void* one(void* args)
{
    Foo* f = (Foo*)args;
    while(true)
    {
        sleep(1);
        f->one();
        pthread_cond_signal(&cond2);
    }
}

void* two(void* args)
{
    Foo* f = (Foo*)args;
    while(true)
    {
        pthread_cond_wait(&cond2,&mutex);
        f->two();
        pthread_cond_signal(&cond3);
    }

}
void* three(void* args)
{
    Foo* f = (Foo*)args;
    while(true)
    {
        pthread_cond_wait(&cond3,&mutex);
        f->three();
    }
}
int main()
{

    pthread_t A, B, C;
    Foo* f = new Foo;
    pthread_create(&A, nullptr, one, (void*)f);
    pthread_create(&B, nullptr, two, (void*)f);
    pthread_create(&C, nullptr, three, (void*)f);
    pthread_cond_init(&cond2,nullptr);
    pthread_cond_init(&cond3,nullptr);
    pthread_mutex_init(&mutex,nullptr);

    pthread_join(A,nullptr);
    pthread_join(B,nullptr);
    pthread_join(C,nullptr);
    pthread_cond_destroy(&cond2);
    pthread_cond_destroy(&cond3);
    pthread_mutex_destroy(&mutex);
    return 0;
}
